﻿using GalaSoft.MvvmLight.Command;
using System;
using System.Windows.Input;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace Reuse.WS.Common {
	public static class ListViewItemClickedToCommand {
		public static DependencyProperty CommandProperty =
            DependencyProperty.RegisterAttached( "Command", typeof( ICommand ), typeof( ListViewItemClickedToCommand ), new PropertyMetadata( null, OnCommandChanged ) );
		public static DependencyProperty ClickedItemProperty =
            DependencyProperty.RegisterAttached( "ClickedItem", typeof( object ), typeof( ListViewItemClickedToCommand ), new PropertyMetadata( null, OnClickedItemChanged ) );
		public static DependencyProperty CommandParameterProperty =
            DependencyProperty.RegisterAttached( "CommandParameter", typeof( object ), typeof( ListViewItemClickedToCommand ), new PropertyMetadata( null) );

		public static void SetCommand( DependencyObject sender, ICommand Command ) {
			if ( sender == null ) {
				return;
			}

			sender.SetValue( CommandProperty, Command );
		}

		public static ICommand GetCommand( DependencyObject sender ) {
			if ( sender == null ) {
				return null;
			}

			return (ICommand)sender.GetValue( CommandProperty );
		}

		public static void SetCommandParameter( DependencyObject sender, object CommandParameter ) {
			if ( sender == null ) {
				return;
			}

			sender.SetValue( CommandParameterProperty, CommandParameter );
		}

		public static object GetCommandParameter( DependencyObject sender ) {
			if ( sender == null ) {
				return null;
			}

			return sender.GetValue( CommandParameterProperty );
		}

		public static void SetClickedItem( DependencyObject sender, object ClickedItem ) {
			if ( sender == null ) {
				return;
			}

			sender.SetValue( ClickedItemProperty, ClickedItem );
		}

		public static object GetClickedItem( DependencyObject sender ) {
			if ( sender == null ) {
				return null;
			}

			return (object)sender.GetValue( ClickedItemProperty );
		}

		private static void OnCommandChanged( DependencyObject d, DependencyPropertyChangedEventArgs args ) {
			ListViewBase listView = (ListViewBase)d;

			if ( listView != null ) {
				listView.ItemClick += listView_ItemClick;
				listView.Unloaded += listView_Unloaded;
			}
		}

		private static void OnClickedItemChanged( DependencyObject d, DependencyPropertyChangedEventArgs args ) {
		}

		static void listView_Unloaded( object sender, RoutedEventArgs e ) {
            //ListViewBase listView = (ListViewBase)sender;
            //listView.ItemClick -= listView_ItemClick;
            //listView.Unloaded -= listView_Unloaded;
		}

		static void listView_ItemClick( object sender, ItemClickEventArgs e ) {
			var listView = (ListViewBase)sender;

			listView.SetValue( ClickedItemProperty, e.ClickedItem );

			var command = listView.GetValue( CommandProperty );
			if ( command != null ) {
				ICommand Command = (ICommand)listView.GetValue( CommandProperty );

				var param = listView.GetValue( CommandParameterProperty );
				if ( param == null ) {
					Command.Execute( e.ClickedItem );
				} else {
					Command.Execute( param );
				}
			}
		}
	}
}
